import {
	getToken,
} from "@/api/login/index.js"
import {
	setToken,
	setUserInfo,
} from "@/utils/storage.js"

export function autoLogin() {

	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				console.log(loginRes, "loginRes");
				if (loginRes.code) {
					// console.log(userInfo)
					let params = {
						code: loginRes.code,
						nickName:"微信用户"+uni.$uv.guid(6),
						avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
					}
					getToken(params).then(result => {
						// console.log(result, "后台获取的数据")
						uni.removeStorageSync('token');
						if (result.data) {
							
							try {
								setToken(result.data.state)
							} catch (e) {
								console.log(e, "存入token失败")
							}
							beforeSetUserInfo(result.data.memberDO)
							resolve(true)
						} else {
							reject()
						}
					}).catch(() => {
						reject()
					})
				} else {
					reject()
				}
			},
			fail() {
				reject()
			}
		});
	})
}

function beforeSetUserInfo(data) {
	console.log(data, "data-")

	setUserInfo(data)
}
